const db = wx.cloud.database()
Page({
  data: {
    res: {},
    index: '0',
    picker: ['1', '2', '3', '4', '5'],
    phone: wx.getStorageSync('phone'),
    loginId: wx.getStorageSync('loginId'),
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  getNowDate: function () {
    var date = new Date();
    var year = date.getFullYear() //年
    var month = date.getMonth() + 1 //月
    var day = date.getDate() //日
    var hour = date.getHours() //时
    var minute = date.getMinutes() //分
    var second = date.getSeconds() //秒
    var xiaoshi = "";
    if (hour < 10) {
      xiaoshi = "0" + hour;
    } else {
      xiaoshi = hour + "";
    }
    var fenzhong = "";
    if (minute < 10) {
      fenzhong = "0" + minute;
    } else {
      fenzhong = minute + "";
    }
    var miao = "";
    if (second < 10) {
      miao = "0" + second;
    } else {
      miao = second + "";
    }
    var time = year + '-' + month + '-' + day + ' ' + xiaoshi + ':' + fenzhong + ':' + miao
    return time
  },
  // 支付操作
  onSubmit() {
    wx.showLoading({
      title: '正在操作',
    })
    db.collection("tjorderList").add({
      data: {
        phone: this.data.phone,
        loginId: this.data.loginId,
        tuanjianlist:this.data.res,
        userlist:wx.getStorageSync('usermsg'),
        tuanjianid:this.data.res._id,
        time:this.getNowDate(),
        money:this.data.res.aprice*this.data.picker[this.data.index],
        peoplenum:this.data.picker[this.data.index],
        isuse:false
      }
    }).then(res=>{
      wx.showToast({
        title: '支付成功',
      })
      setTimeout(() => {
        wx.reLaunch({
          url: '../index/index',
        })
      }, 1000);
    })
  },
  onLoad(e) {
    var id = e.id
    wx.cloud.callFunction({
      name: "gettjbyid",
      data: {
        id
      }
    }).then(res => {
      console.log(res)
      this.setData({
        res: res.result.data
      })
    })
  }
})